package com.buaa.xxx0319;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;



public class MainActivity extends AppCompatActivity {

    private SeekBar sb;
    private Handler handler;
    private boolean isPause = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sb = (SeekBar)findViewById(R.id.sb);
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                sb.setProgress(msg.getData().getInt("pro"));
            }
        };
    }

    public void play(View view) {
        new Thread() {
            @Override
            public void run() {
                synchronized (handler) {
                    for(int i=0;i<100;i++) {
                        if(isPause) {
                            try {
                                handler.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        try {
                            Thread.sleep(20);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        Message msg = new Message();
                        Bundle bundle = new Bundle();
                        bundle.putInt("pro",i);
                        msg.setData(bundle);
                        handler.sendMessage(msg);
                    }
                }
            }
        }.start();
    }

    public void pause(View view) {
        isPause = true;
    }

    public void resume(View view) {
        synchronized (handler) {
            isPause = false;
            handler.notify();
        }
    }
}
